<?php

namespace App\Http\Controllers\Admin\shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Brand;

class BrandController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $brands= Brand::orderBy('sort_order')->paginate(8);

        return view('admin.brand.index')->with('brands',$brands);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.brand.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Brand::create($request->all());
        return redirect(route('brands.index'));

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Brand::destroy($id);
        return redirect(route('brands.index'));
    }
    public function is_show(Request $request)
    {
        $id=$request->id;
        $brand=Brand::find($id);
        $brand->is_show=!$brand->is_show;
        $brand->save();
    }
    public function sort_order(Request $request)
    {
        $messages = [
            'sort_order.max' => '排序字段不能超过2位',
            'sort_order.integer' => '必须是整数',
        ];
        $rules = [
            'sort_order' => 'required|integer|max:99'
        ];
        $this->validate($request, $rules, $messages);

        $id = $request->id;
        $brand = Brand::find($id);
        $brand->sort_order = $request->sort_order;
        $brand->save();
    }
    public function destroy_all(Request $request){

        $id=$request->checked_id;
        Brand::destroy($id);

    }

}
